<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- unix-signal-handlers.qdoc -->
  <title>Calling Qt Functions From Unix Signal Handlers | Qt 5.14</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt 5.14</a></td><td >Calling Qt Functions From Unix Signal Handlers</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.14.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Calling Qt Functions From Unix Signal Handlers</h1>
<span class="subtitle"></span>
<!-- $$$unix-signals.html-description -->
<div class="descr"> <a name="details"></a>
<p>You <i>can't</i> call Qt functions from Unix signal handlers. The standard POSIX rule applies: You can only call async-signal-safe functions from signal handlers. See <a href="http://www.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_04.html#tag_02_04_01">Signal Actions</a> for the complete list of functions you can call from Unix signal handlers.</p>
<p>But don't despair, there is a way to use Unix signal handlers with Qt. The strategy is to have your Unix signal handler do something that will eventually cause a Qt signal to be emitted, and then you simply return from your Unix signal handler. Back in your Qt program, that Qt signal gets emitted and then received by your Qt slot function, where you can safely do whatever Qt stuff you weren't allowed to do in the Unix signal handler.</p>
<p>One simple way to make this happen is to declare a socket pair in your class for each Unix signal you want to handle. The socket pairs are declared as static data members. You also create a <a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a> to monitor the <i>read</i> end of each socket pair, declare your Unix signal handlers to be static class methods, and declare a slot function corresponding to each of your Unix signal handlers. In this example, we intend to handle both the SIGHUP and SIGTERM signals. Note: You should read the socketpair(2) and the sigaction(2) man pages before plowing through the following code snippets.</p>
<pre class="cpp">

  <span class="keyword">class</span> MyDaemon : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>
  {
      Q_OBJECT

    <span class="keyword">public</span>:
      MyDaemon(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
      <span class="operator">~</span>MyDaemon();

      <span class="comment">// Unix signal handlers.</span>
      <span class="keyword">static</span> <span class="type">void</span> hupSignalHandler(<span class="type">int</span> unused);
      <span class="keyword">static</span> <span class="type">void</span> termSignalHandler(<span class="type">int</span> unused);

    <span class="keyword">public</span> <span class="keyword">slots</span>:
      <span class="comment">// Qt signal handlers.</span>
      <span class="type">void</span> handleSigHup();
      <span class="type">void</span> handleSigTerm();

    <span class="keyword">private</span>:
      <span class="keyword">static</span> <span class="type">int</span> sighupFd<span class="operator">[</span><span class="number">2</span><span class="operator">]</span>;
      <span class="keyword">static</span> <span class="type">int</span> sigtermFd<span class="operator">[</span><span class="number">2</span><span class="operator">]</span>;

      <span class="type"><a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a></span> <span class="operator">*</span>snHup;
      <span class="type"><a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a></span> <span class="operator">*</span>snTerm;
  };

</pre>
<p>In the MyDaemon constructor, use the socketpair(2) function to initialize each file descriptor pair, and then create the <a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a> to monitor the <i>read</i> end of each pair. The activated() signal of each <a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a> is connected to the appropriate slot function, which effectively converts the Unix signal to the <a href="../qtcore/qsocketnotifier.html#activated">QSocketNotifier::activated</a>() signal.</p>
<pre class="cpp">

  MyDaemon<span class="operator">::</span>MyDaemon(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent)
          : <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>(parent)
  {
      <span class="keyword">if</span> (<span class="operator">::</span>socketpair(AF_UNIX<span class="operator">,</span> SOCK_STREAM<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> sighupFd))
         <a href="../qtcore/qtglobal.html#qFatal">qFatal</a>(<span class="string">&quot;Couldn't create HUP socketpair&quot;</span>);

      <span class="keyword">if</span> (<span class="operator">::</span>socketpair(AF_UNIX<span class="operator">,</span> SOCK_STREAM<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> sigtermFd))
         <a href="../qtcore/qtglobal.html#qFatal">qFatal</a>(<span class="string">&quot;Couldn't create TERM socketpair&quot;</span>);
      snHup <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a></span>(sighupFd<span class="operator">[</span><span class="number">1</span><span class="operator">]</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a></span><span class="operator">::</span>Read<span class="operator">,</span> <span class="keyword">this</span>);
      connect(snHup<span class="operator">,</span> SIGNAL(activated(<span class="type">int</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(handleSigHup()));
      snTerm <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a></span>(sigtermFd<span class="operator">[</span><span class="number">1</span><span class="operator">]</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a></span><span class="operator">::</span>Read<span class="operator">,</span> <span class="keyword">this</span>);
      connect(snTerm<span class="operator">,</span> SIGNAL(activated(<span class="type">int</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(handleSigTerm()));

      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
  }

</pre>
<p>Somewhere else in your startup code, you install your Unix signal handlers with sigaction(2).</p>
<pre class="cpp">

  <span class="keyword">static</span> <span class="type">int</span> setup_unix_signal_handlers()
  {
      <span class="keyword">struct</span> sigaction hup<span class="operator">,</span> term;

      hup<span class="operator">.</span>sa_handler <span class="operator">=</span> MyDaemon<span class="operator">::</span>hupSignalHandler;
      sigemptyset(<span class="operator">&amp;</span>hup<span class="operator">.</span>sa_mask);
      hup<span class="operator">.</span>sa_flags <span class="operator">=</span> <span class="number">0</span>;
      hup<span class="operator">.</span>sa_flags <span class="operator">|</span><span class="operator">=</span> SA_RESTART;

      <span class="keyword">if</span> (sigaction(SIGHUP<span class="operator">,</span> <span class="operator">&amp;</span>hup<span class="operator">,</span> <span class="number">0</span>))
         <span class="keyword">return</span> <span class="number">1</span>;

      term<span class="operator">.</span>sa_handler <span class="operator">=</span> MyDaemon<span class="operator">::</span>termSignalHandler;
      sigemptyset(<span class="operator">&amp;</span>term<span class="operator">.</span>sa_mask);
      term<span class="operator">.</span>sa_flags <span class="operator">|</span><span class="operator">=</span> SA_RESTART;

      <span class="keyword">if</span> (sigaction(SIGTERM<span class="operator">,</span> <span class="operator">&amp;</span>term<span class="operator">,</span> <span class="number">0</span>))
         <span class="keyword">return</span> <span class="number">2</span>;

      <span class="keyword">return</span> <span class="number">0</span>;
  }

</pre>
<p>In your Unix signal handlers, you write a byte to the <i>write</i> end of a socket pair and return. This will cause the corresponding <a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a> to emit its activated() signal, which will in turn cause the appropriate Qt slot function to run.</p>
<pre class="cpp">

  <span class="type">void</span> MyDaemon<span class="operator">::</span>hupSignalHandler(<span class="type">int</span>)
  {
      <span class="type">char</span> a <span class="operator">=</span> <span class="number">1</span>;
      <span class="operator">::</span>write(sighupFd<span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">,</span> <span class="operator">&amp;</span>a<span class="operator">,</span> <span class="keyword">sizeof</span>(a));
  }

  <span class="type">void</span> MyDaemon<span class="operator">::</span>termSignalHandler(<span class="type">int</span>)
  {
      <span class="type">char</span> a <span class="operator">=</span> <span class="number">1</span>;
      <span class="operator">::</span>write(sigtermFd<span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">,</span> <span class="operator">&amp;</span>a<span class="operator">,</span> <span class="keyword">sizeof</span>(a));
  }

</pre>
<p>In the slot functions connected to the <a href="../qtcore/qsocketnotifier.html#activated">QSocketNotifier::activated</a>() signals, you <i>read</i> the byte. Now you are safely back in Qt with your signal, and you can do all the Qt stuff you weren'tr allowed to do in the Unix signal handler.</p>
<pre class="cpp">

  <span class="type">void</span> MyDaemon<span class="operator">::</span>handleSigTerm()
  {
      snTerm<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
      <span class="type">char</span> tmp;
      <span class="operator">::</span>read(sigtermFd<span class="operator">[</span><span class="number">1</span><span class="operator">]</span><span class="operator">,</span> <span class="operator">&amp;</span>tmp<span class="operator">,</span> <span class="keyword">sizeof</span>(tmp));

      <span class="comment">// do Qt stuff</span>

      snTerm<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);
  }

  <span class="type">void</span> MyDaemon<span class="operator">::</span>handleSigHup()
  {
      snHup<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
      <span class="type">char</span> tmp;
      <span class="operator">::</span>read(sighupFd<span class="operator">[</span><span class="number">1</span><span class="operator">]</span><span class="operator">,</span> <span class="operator">&amp;</span>tmp<span class="operator">,</span> <span class="keyword">sizeof</span>(tmp));

      <span class="comment">// do Qt stuff</span>

      snHup<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);
  }

</pre>
</div>
<!-- @@@unix-signals.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
